
var ming
var num1 = 1
if (localStorage.getItem("id") && localStorage.getItem("token")) {
   
    fn()
    // 默认
    fun()
    // 修改密码
    $(".mi").on("click",function(){
        console.log(123);
        $(".a>div").html(`
        <form action="" class="form1">
            <table  style="height:144px;">
                <tr>
                    <td>旧密码</td>
                    <td><input type="password" value="" class="a1"></td>
                </tr>
                <tr >
                    <td>新密码</td>
                    <td><input type="password" class="a2"></td>
                </tr>
                <tr >
                <td>确认密码</td>
                <td><input type="password" class="a3"></td>
            </tr>
            </table>
            <input type="submit" value="确定">
        </form>

        `)
        $("form").submit(function(){
            var a1 = $(".a1")[0].value
            var a2 = $(".a2")[0].value
            var a3 = $(".a3")[0].value
            $.ajax({
                url:"http://localhost:8888/users/rpwd",
                method:"post",
                success:function(res){
                    if(res.code===1){
                        alert(res.message)
                        location.href="login.html"
                    }
                },
                headers:{
                    authorization:localStorage.getItem("token")
                },
                data:{
                    id:localStorage.getItem("id"),
                    oldPassword:a1,
                    newPassword:a2,
                    rNewPassword:a3,
                }
            })
            return false
        })
    })
    $(".xin").on("click",function(){
        console.log(123);
        $(".a>div").html(`
        <form action="" class="form1">
            <table>
                <tr>
                    <td>用户名</td>
                    <td><input type="text" value=""></td>
                </tr>
                <tr>
                    <td>年龄</td>
                    <td><input type="text" class="a1"></td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td style="color: #999999;"><input type="radio" name="gender">男<input type="radio" name="gender">女</td>
                </tr>
                <tr>
                    <td>昵称</td>
                    <td><input type="text" class="a2"></td>
                </tr>
                <tr>
                    <td>身份</td>
                    <td><input type="text" class="a3"></td>
                </tr>
            </table>
            <input type="submit" value="确定">
        </form>
        `)
        fun()
    })
} else {
    
}

function fn() {
    $.ajax({
        url: "http://localhost:8888/users/info",
        method: "get",
        data: {
            id: localStorage.getItem("id") 
        },
        headers: {
            authorization: localStorage.getItem("token") 
        },
        success: function (res) {
            console.log(res);
            ming = res.info.username
            yonghu(res.info.nickname)
            tuichu()

        }
    })
    // 获取信息
    function yonghu(a) {
        $(".header-left").html(`
        <span>您好! ${a} </span>
        <a href="login.html">退出</a>
    
        `)
    }
    // 注销登录   商品id为啥删不掉
    function tuichu() {
        $(".header-left>a").on("click", function () {
            $.ajax({
                url: "http://localhost:8888/users/logout",
                method: "get",
                headers: {
                    authorization:localStorage.getItem("token") 
                },
                data: {
                    id:  localStorage.getItem("id") 
                },
                success: function (res) {
                    console.log(res);
                    alert(res.message)
                    localStorage.removeItem("id")
                    localStorage.removeItem("gods_id")
                    localStorage.removeItem("token")
                    location.href = "login.html"
                }
            })
            return false
        })
    }
}

function fun(){
    $("form").submit(function(){
        var Age = $(".a1")[0].value
        var Nickname = $(".a2")[0].value
        var Shen = $(".a3")[0].value
        if($("[type='radio']")[0].checked){
            var Gender = "男"
        }else if($("[type='radio']")[1].checked){
            var Gender = "女"
        }else{
            console.log("性别未选");
        }
        $.ajax({
            url:"http://localhost:8888/users/update",
            method:"post",
            data:{
                id:localStorage.getItem("id"),
                age:Age,
                gender:Gender,
                nickname:Nickname,
                identity:Shen,
            },
            headers:{
                authorization:localStorage.getItem("token")
            },
            success:function(res){
                alert(res.message)
                fn()
                $(".a1")[0].value=$(".a2")[0].value=Shen = $(".a3")[0].value=``
                $("[type='radio']")[0].checked= $("[type='radio']")[1].checked=false
            }
        })
        return false
    })
}